package com.gao.exception;

import static com.gao.exception.ProductException.ErrorCode.OK;

/**
 * @author gao
 * @time 2022/11/25 11:30:40
 */
public class ProductException extends RuntimeException {

    private Object[] datas = new Object[3];
    private ErrorCode errorCode = OK;

    public enum ErrorCode {
        OK, PRODUCT_NOT_FOUND, STOCK_NOT_ENOUGH
    }

    public ProductException() {
    }

    public ProductException(String message) {
        super(message);
    }

    public ProductException(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    public ProductException(ErrorCode errorCode, Integer errorProductId) {
        this.errorCode = errorCode;
        this.datas[0] = errorProductId;
    }

    public ProductException(ErrorCode errorCode, Integer exceptedProductId, Integer exceptedProductNumber, Integer realProductStock) {
        this.datas[0] = exceptedProductId;
        this.datas[1] = exceptedProductNumber;
        this.datas[2] = realProductStock;
        this.errorCode = errorCode;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    @Override
    public String getMessage() {
        switch (errorCode) {
            case OK:
                return "Should not get here.";
            case PRODUCT_NOT_FOUND:
                return String.format("Product %d is not found.", datas[0]);
            case STOCK_NOT_ENOUGH:
                return String.format("Product %d is out of stock. except %d but only %d available.",
                        datas[0], datas[1], datas[2]);
            default:
        }
        return "";
    }
}
